package com.angel.sensors.connection;

import java.util.ArrayList;
import java.util.List;

public class Connection {

    private String mName = "";
    
    public enum State {
        CONNECTED,
        DISCONNECTED
    }
    
    public enum Type {        
        NUMBER,
        PASSWORD
    }
    
    public class Field {
        Type type;
        String name;
        String value;
    }
        
    
    public void setName(String name) { mName = name; }
    public String getName() { return mName; }
    
    public List<Field> getFields() {
        return null;
    }
    
    public void open_() {}
    public void close_() {}
    
    public State getState() { return mState; }
    
    
    protected void setState(State state) {
        if (state != mState) {
            mState = state;
            invokeListeners(this);
        }
    }
    
    public void addConnectionListener(ConnectionListener l) {
        if (!mConnectionListeners.contains(l)) {
            mConnectionListeners.add(l);
        }
    }
    
    public void removeConnectionListener(ConnectionListener l) {
        mConnectionListeners.remove(l);
    }
    
    
    private void invokeListeners(Connection connection) {
        for (ConnectionListener l: mConnectionListeners) {
            l.stateChanged(connection);
        }
    }
    
    private State mState = State.DISCONNECTED;
    
    private ArrayList<ConnectionListener> mConnectionListeners = new ArrayList<ConnectionListener>();
    
    
}
